﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("B_Book")]
    public class B_Book : IEntity<B_Book>
    {
        #region
        [Column(TypeName = "int")]
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Name { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Authors { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Translator { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Press { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string ISBN { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string FXFW { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string BC { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string YC { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string KB { get; set; }
        public int Pages { get; set; }
        public int Characters { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string CYZ { get; set; }
        public decimal Price { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Buy { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string CoverImg { get; set; }
        [Column(TypeName="datetime")]
        public DateTime PubDate { get; set; }
        [Column(TypeName = "datetime")]
        public DateTime PrintDate { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(16)]
        public string Pack { get; set; }
        [Column(TypeName = "text")]
        public string Introduction { get; set; }
        [Column(TypeName="text")]
        public string Content { get; set; }
        [Column(TypeName="text")]
        public string AboutAuthor { get; set; }
        [Column(TypeName="text")]
        public string Comments { get; set; }
        #endregion

        public override B_Book Find(string key)
        {
            int id = key.ToInt32();
            return Find(c=>c.Id == id);
        }

        public override B_Book Find(System.Linq.Expressions.Expression<Func<B_Book, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Book.FirstOrDefault(where);
                if (item != null) 
                {
                    this.Authors = item.Authors;
                    this.BC = item.BC;
                    this.Characters = item.Characters;
                    this.CoverImg = item.CoverImg;
                    this.CYZ = item.CYZ;
                    this.FXFW = item.FXFW;
                    this.Id = item.Id;
                    this.Introduction = item.Introduction;
                    this.ISBN = item.ISBN;
                    this.KB = item.KB;
                    this.Name = item.Name;
                    this.Pages = item.Pages;
                    this.Price = item.Price;
                    this.Press = item.Press;
                    this.Translator = item.Translator;
                    this.Buy = item.Buy;
                    this.YC = item.YC;
                    this.PubDate = item.PubDate;
                    this.PrintDate = item.PrintDate;
                    this.Pack = item.Pack;
                    this.Content = item.Content;
                    this.AboutAuthor = item.AboutAuthor;
                    this.Comments = item.Comments;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<B_Book, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Book.Any(where);
            }
        }
    }
}
